
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'en_us.dart';
import 'zh_cn.dart';
import 'zh_tw.dart';

/// 多语言翻译管理
class TranslationManager extends Translations {
  TranslationManager._internal();
  static final TranslationManager _instance = TranslationManager._internal();
  factory TranslationManager() => _instance;

  /// 当前使用的语言
  Locale currentLocale = locale;

  /// 支持的语言列表
  static const enUS = Locale('en', 'US');
  static const zhCN = Locale('zh', 'CN');
  static const zhTW = Locale('zh', 'TW');
  static final supportedLocales = [enUS, zhCN, zhTW];

  /// 默认语言
  static const locale = enUS;

  /// 回退语言
  static const fallbackLocale = enUS;

  /// 获取设备的默认语言(不在支持列表中则使用默认语言)
  static Future<Locale> getDeviceLocale() async {
    // 优先查询用户保存的语言
    final savedLocale = await getSavedLocale();
    if (savedLocale != null) {
      TranslationManager().currentLocale = savedLocale;
      return savedLocale;
    }

    // 如果没有保存的语言，则使用设备的默认语言
    // Locale deviceLocale = Get.deviceLocale ?? locale;
    // if (supportedLocales.contains(deviceLocale)) {
    //   TranslationManager().currentLocale = deviceLocale;
    //   return deviceLocale;
    // }

    // 如果没有保存的语言，则使用设备语言(必须是支持的语言)
    Locale? deviceLocale = Get.deviceLocale;
    if (deviceLocale != null) {
      String languageCode = deviceLocale.languageCode;
      String? countryCode = deviceLocale.countryCode;

      Locale? sysLocale;
      if (languageCode == 'en') {
        sysLocale = enUS;
      } else if (languageCode == 'zh') {
        if (countryCode == 'TW' || countryCode == 'HK') {
          sysLocale = zhTW;
        } else {
          sysLocale = zhCN;
        }
      }
      if (sysLocale != null) {
        TranslationManager().currentLocale = sysLocale;
        return sysLocale;
      }
    }

    // 如果设置语言不在支持的语言列表中，则指定默认语言
    TranslationManager().currentLocale = locale;
    return locale;
  }

  /// 更新语言(仅仅支持支持语言列表中的几种)
  static Future<void> changeLocale(Locale locale) async {
    if (supportedLocales.contains(locale)) {
      await saveLocale(locale);
      Get.updateLocale(locale);
      TranslationManager().currentLocale = locale;
    }
  }

  /// 保存语言偏好到本地
  static Future<void> saveLocale(Locale locale) async {
  //  XCache.getInstance().setString('languageCode', locale.languageCode);
  //  XCache.getInstance().setString('countryCode', locale.countryCode ?? '');
  }

  /// 获取保存的语言偏好
  static Future<Locale?> getSavedLocale() async {
  //  final languageCode = XCache.getInstance().get('languageCode');
  //  final countryCode = XCache.getInstance().get('countryCode');
  //
  //   if (languageCode != null && countryCode != null) {
  //     final savedLocale = Locale(languageCode, countryCode);
  //     if (supportedLocales.contains(savedLocale)) {
  //       return savedLocale;
  //     }
  //   }
    return null;
  }

  @override
  Map<String, Map<String, String>> get keys => {
        'en_US': enUsMap,
        'zh_CN': zhCnMap,
        'zh_TW': zhTwMap,
      };
}
